package com.hdax.dm.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hdax.dm.dto.CommonResponse;
import com.hdax.dm.dto.DtoUtil;
import com.hdax.dm.dto.user.TokenDto;
import com.hdax.dm.dto.user.UserInfoDto;
import com.hdax.dm.entity.base.DmImage;
import com.hdax.dm.entity.user.DmUser;
import com.hdax.dm.feign.base.DmImageFeign;
import com.hdax.dm.feign.user.DmUserFeign;
import com.hdax.dm.utils.MokeImage;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;


@RestController
@RequestMapping(path = "/order")
public class DmPersonalController {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private DmImageFeign imageFeign;
    @Autowired
    private DmUserFeign userFeign;
    private Long userId;

    //返回用户信息
    @PostMapping(path = "/permission/querypersoninfo")
    public CommonResponse<UserInfoDto> querypersoninfo(HttpServletRequest request) throws JsonProcessingException {
        //获取用户token信息
        String token = request.getHeader("token");
        ObjectMapper objectMapper = new ObjectMapper();
        DmUser dmUser = objectMapper.readValue(redisTemplate.opsForValue().get(token),DmUser.class);
        userId = dmUser.getId();

        //拷贝用户信息
        UserInfoDto userInfoDto = new UserInfoDto();
        DmImage dmImage = imageFeign.getImage(0L,0L,dmUser.getId());
        DmUser dmUser1 = userFeign.findById(dmUser.getId());
        BeanUtils.copyProperties(dmUser1,userInfoDto);
        userInfoDto.setHeadUrl(dmImage!=null?dmImage.getImgUrl(): MokeImage.getImage(4));
        userInfoDto.setInterest(dmUser1.getHobby());
        userInfoDto.setSex(dmUser1.getSex());

        return DtoUtil.returnSuccess(userInfoDto);
    }

    //修改用户信息
    @PostMapping(path = "/permission/modifypersoninfo")
    public CommonResponse<String> modifypersoninfo(@RequestBody UserInfoDto userInfoDto,
                                                   HttpServletRequest request) throws JsonProcessingException {
        //获取用户token信息
        String token = request.getHeader("token");
        ObjectMapper objectMapper = new ObjectMapper();
        DmUser dmUser = objectMapper.readValue(redisTemplate.opsForValue().get(token),DmUser.class);

        //如果其他信息为空，则只修改爱好信息
        if (userInfoDto.getRealName()==null){
            dmUser.setHobby(userInfoDto.getInterest());
            userFeign.updateUserInfo(dmUser);
            return DtoUtil.returnSuccess("修改成功");
        }

        //拷贝用户信息
        dmUser.setBirthday(userInfoDto.getBirthday());
        BeanUtils.copyProperties(userInfoDto,dmUser);
        dmUser.setHobby(userInfoDto.getInterest());
        //截取年月日设置为用户生日  2021-07-16 15:00:00 > 2021-07-16
        if (userInfoDto.getBirthday()!=null&&!userInfoDto.getBirthday().equals("")){
            dmUser.setBirthday(userInfoDto.getBirthday().replace("T"," ").substring(0,18));
        }

        //完成修改
        userFeign.updateUserInfo(dmUser);
        return DtoUtil.returnSuccess("修改成功");
    }

    @PostMapping(path = "/updateImg")
    private CommonResponse<String> updateImg(@RequestParam("file") MultipartFile file) {

        System.out.println("修改");
        if (file.isEmpty()) {
            return DtoUtil.returnFail("0001","上传失败，请选择文件");
        }

        //生成随机名字
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("YYYYMMDDhhmmss");
        int ran = (int) (Math.random()*(9999-1000)+1000);
        //C:\Users\86176\桌面\大觅网\大觅网提供资料\Vue前端项目\dmCode1\static\img\
        //E:/ceshiimg/
//        String filePath = "C:/Users/86176/桌面/大觅网/大觅网提供资料/Vue前端项目/dmCode1/static/img/"+ format.format(date)+ran+".jpg";
        String filePath = "C:/Users/86176/桌面/大觅网/大觅网提供资料/Vue前端项目/dmCode/static/img/"+ format.format(date)+ran+".jpg";
        File dest = new File(filePath);

        String subStr = filePath.substring(filePath.length()-35);
        //持久化
        DmImage dmImage = imageFeign.getImage(0L,0L,userId);
        dmImage.setImgUrl(subStr);
        imageFeign.updateImg(dmImage);

        try {
            file.transferTo(dest);
            System.out.println("上传成功");
            return DtoUtil.returnSuccess(subStr);
        } catch (IOException e) {
            System.out.println(e.toString());
        }
        return DtoUtil.returnFail("0002","上传失败");
    }
}
